package com.peralex.utilities.ui.graphs.graphBase;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

/**
 * A panel that displays X and Y co-ordinates of the current mouse position.
 * The coordinates are arranged horizontally, to minimise vertical space.
 * 
 * It has a timer to limit the rate of GUI update events.
 * 
 * @author Noel Grandin
 */
public class CoordinatesPanel extends javax.swing.JPanel
{
	/**
   * Timer to delay setting of Coordinate Labels.
   */
	private final Timer oCoordinateLabelTimer;

	private float fXVal;
	private float fYVal;
	
  /** Creates new form cCoordinatesPanel */
  public CoordinatesPanel()
  {
    initComponents();
    
    // set up defaults that work well for our default graphs i.e. frequency vs. dbm
    setXCoordinateDecimalFormat("   0.0000");
    setYCoordinateDecimalFormat("  0.0");
    
		oCoordinateLabelTimer = new javax.swing.Timer(150, new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				oXCoordinateValue.setValue(fXVal);
				oYCoordinateValue.setValue(fYVal);
			}
		});
		oCoordinateLabelTimer.setRepeats(false);

  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents()
  {
    oXCoordinateLabel = new javax.swing.JLabel();
    oXCoordinateValue = new com.peralex.utilities.ui.FixedSizeNumberLabel();
    oYCoordinateLabel = new javax.swing.JLabel();
    oYCoordinateValue = new com.peralex.utilities.ui.FixedSizeNumberLabel();

    setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));

    setBackground(new java.awt.Color(255, 255, 255));
    setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    oXCoordinateLabel.setText(" X: ");
    add(oXCoordinateLabel);

    oXCoordinateValue.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    oXCoordinateValue.setValue(0.0);
    add(oXCoordinateValue);

    oYCoordinateLabel.setText(" Y: ");
    add(oYCoordinateLabel);

    oYCoordinateValue.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    oYCoordinateValue.setValue(0.0);
    add(oYCoordinateValue);

  }// </editor-fold>//GEN-END:initComponents
  
  @Override
  public void setFont(Font font)
  {
  	super.setFont(font);
  	// have to check for null because we get called during construction
  	if (oXCoordinateLabel!=null)
  	{
			oXCoordinateLabel.setFont(font);
			oXCoordinateValue.setFont(font);
			oYCoordinateLabel.setFont(font);
			oYCoordinateValue.setFont(font);
  	}
  }

  public void setCoordinateSuffixes(String xSuffix, String ySuffix)
  {
  	oXCoordinateValue.setSuffix(xSuffix);
  	oYCoordinateValue.setSuffix(ySuffix);
  }
  
  public void setCoordinates(float fXVal, float fYVal)
  {
		this.fXVal = fXVal;
		this.fYVal = fYVal;
		oCoordinateLabelTimer.start();
  }
  
  public void setXCoordinate(float fXVal)
  {
		this.fXVal = fXVal;
		oCoordinateLabelTimer.start();
  }
  
  public void setYCoordinate(float fYVal)
  {
		this.fYVal = fYVal;
		oCoordinateLabelTimer.start();
  }
  
	public final void setXCoordinateDecimalFormat(String oFormat)
	{
		oXCoordinateValue.setFormat(oFormat);
	}

	public final void setYCoordinateDecimalFormat(String oFormat)
	{
		oYCoordinateValue.setFormat(oFormat);
	}

	public void setXCoordinateVisible(boolean bVisible)
	{
		oXCoordinateLabel.setVisible(bVisible);
		oXCoordinateValue.setVisible(bVisible);
	}
	
	public void setYCoordinateVisible(boolean bVisible)
	{
		oYCoordinateLabel.setVisible(bVisible);
		oYCoordinateValue.setVisible(bVisible);
	}
	
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel oXCoordinateLabel;
  private com.peralex.utilities.ui.FixedSizeNumberLabel oXCoordinateValue;
  private javax.swing.JLabel oYCoordinateLabel;
  private com.peralex.utilities.ui.FixedSizeNumberLabel oYCoordinateValue;
  // End of variables declaration//GEN-END:variables
  
}
